<?php

	require_once('../functions/mainFns.php');
	session_start();

	$act 		= @$_REQUEST['act'];
	$title 		= @$_REQUEST['title'];
	$content 	= @$_REQUEST['content'];
	$imgid 		= @$_REQUEST['imgid'];
	$imgsrc 	= @$_REQUEST['imgsrc'];
	$postid 	= @$_REQUEST['postid'];
	$replyid 	= @$_REQUEST['replyid'];
	$startnum 	= @$_REQUEST['startnum'];
	$showtime 	= @$_REQUEST['showtime'];
	$postsid 	= @$_REQUEST['postsid'];
	$userid1 	= @$_REQUEST['userid1'];
	$userid2 	= @$_REQUEST['userid2'];
	$userid		= @$_SESSION['userid'];

//如果是新发帖
if($act == 'newpost'){
	$content = urlencode($content);
	$title 	 = urlencode($title);
	$ds 	 = Dataservice::getInstance();
	$res 	 = $ds->newpost($title,$content,$userid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$postid  = @$res[1]	-> info->id;
	if($status == 'fail'){
		echo '0';
	}else if($status == 'success'){
		echo $postid;
	}
}

//如果得到帖子的细节
if($act == 'getpostdetail'){
	$ds 	 = Dataservice::getInstance();
	$res 	 = $ds->getpostdetail($postid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$info    = @$res[1]	-> info;
	$array = array(
		"title" 	  => $info->title,
		"content" 	  => $info->content,
		"update_time" => $info->update_time,
		"postuserid"  => $info->user_id
	);
	if($status == 'fail'){
		echo '0';
	}else if($status == 'success'){
		echo json_encode($array);
	}
}


//如果是取得所有问题的内容
if($act == 'getpostlist'){
	$ds  = Dataservice::getInstance();
	$res = $ds->getpostlist($startnum,$showtime);
	$res = json_decode($res);
	$status 	= $res[0] -> status;
	$postlist	= @$res[1] -> info;
	//var_dump($postlist);
	if($postlist){
		foreach($postlist as $key => $value){
			showPostTitleLink($value);
		}
	}
}

//如果新插入图片
if($act == 'newpic'){
	//先将base64-url编码存到本地images/tmp路径下
	$imgsrc = explode('base64,',$imgsrc);
	$base64_img = $imgsrc[1];
	$tmp = base64_decode($base64_img);//解码到临时文件
	$path = "../images/tmp/";
	file_put_contents($path."$imgid.jpg", $tmp);
 	$nowpath = $path.$imgid.".jpg";
	$imgname = $imgid.'.jpg';
	unset($imgsrc);
	unset($base64_img);
	unset($tmp);
	//读取本地图片信息，进行尺寸判断
	$imginfo = getimagesize($nowpath);
	if($imginfo[0] > 800){	
		//得到裁剪图片比例
		$per = 800/$imginfo[0];	
		list($width, $height) = getimagesize($nowpath);
		$n_w=$width*$per;
		$n_h=$height*$per;
		$new=imagecreatetruecolor($n_w, $n_h);
		//根据图片格式，拷贝部分图像并调整
		$imgtype = $imginfo[2];		
		switch($imgtype){
			case 1:
				$img=imagecreatefromgif($nowpath);
				break;		
			case 2:
				$img=imagecreatefromjpeg($nowpath);
				break;		
			case 3:
				$img=imagecreatefrompng($nowpath);
				break;		
			case 4:
				$img=imagecreatefromwbmp($nowpath);
				break;	
			default:
				$img=imagecreatefromjpeg($nowpath);
		}
		imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
		imagejpeg($new,$path.$imgid.'new.jpg');//图像输出新图片、另存为
		imagedestroy($new);
		imagedestroy($img);
		$imgname = $imgid."new.jpg";
	}
	//传输裁剪后或者最终图像  注意！CURL传输文件的时候必须用绝对路径！并且前面加@
 	$nowpath = dirname(__FILE__)."/../images/tmp/".$imgname;
	$ds  = Dataservice::getInstance();
	$res = $ds->newpic($imgid,$nowpath,$userid);
 	$res = json_decode($res);
	$status  =  $res[0]	-> status;
	$id  	 = @$res[1]	-> info->id; 
 	if($status == 'fail'){
		echo '0';
	}else if($status == 'success'){
		echo DATAWEB."images/$userid/tmp/$id.jpg";
	}    
}



//初始化post，将文件夹的图片清理掉
if($act == 'initpost'){
	cleardir();
	$ds  = Dataservice::getInstance();
	$res = $ds->initpost($userid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$id  	 = @$res[1]	-> info->id;
	if($status == 'fail'){
		echo 'post_init_failed!';
	}else if($status == 'success'){
		echo 'post_init_success!';
	}  
}

//处理post的图片的路径，将其从tmp转移到上一级userid的目录下
if($act == 'dealpicpath'){
	$ds  = Dataservice::getInstance();
	$res = $ds->dealpicpath($userid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$id  	 = @$res[1]	-> info->id;
	if($status == 'fail'){
		echo 'dealpicpath_failed!';
	}else if($status == 'success'){
		echo 'dealpicpath_success!';
	}  
}

//回复post内容
if($act == 'postreply'){
	$ds  = Dataservice::getInstance();
	$res = $ds->postreply($postid,$content,$userid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$info  	 = @$res[1]	-> info;
	//var_dump($info);
	$id  	 = @$info->id;
	echo $id;
}

//回复评论，回复post中的reply
if($act == 'reply2sb'){
	$ds  = Dataservice::getInstance();
	$res = $ds->reply2sb($postsid,$content,$userid,$replyid);
	$res = json_decode($res);
	//var_dump($res);
	$status  =  $res[0]	-> status;
	$info  	 = @$res[1]	-> info;
	//var_dump($res);
	$id  	 = @$info->id;
	echo $id;
}

//展示回复细节
if($act == 'showrelydetail'){
	$ds  = Dataservice::getInstance();
	$res = $ds->getreplydetail($replyid);
	$res 	 = json_decode($res);
	$status  =  $res[0]	-> status;
	$info  	 = @$res[1]	-> info;
	showPostReply($info); 
}

//拿到post的全部回复内容replies
if($act == 'getpostallreplies'){
	$ds  = Dataservice::getInstance();
	$res = $ds->getpostallreplies($postid);
	$res 	 = json_decode($res);
	$status  	=  $res[0]	-> status;
	$replylist  = @$res[1]	-> info;
	if($replylist){
		foreach($replylist as $key => $value){
			showPostReply($value);
		}
	}
}


//拿到post下userid1和userid2相关的对话List,先把所有的东西都拿出来吧。。。
if($act == 'getdialoglist'){
	$ds  = Dataservice::getInstance();
	$res = $ds->getdialoglist($postid,$userid1,$userid2);
	$res = json_decode($res);
 	$status  	=  $res[0]	-> status;
	$dialoglist = @$res[1]	-> info;
	$dialoglist_between = $dialoglist->res1;
	$dialoglist_single  = $dialoglist->res2;
	if($dialoglist_between){
		foreach($dialoglist_between as $key => $value){
			showPostReply($value,0);
		}
	} 
	showPostReply($dialoglist_single,0);
}




?>